package com.jd.service.impl;

import com.alibaba.fastjson.JSON;
import com.jd.common.Page;
import com.jd.dao.CategoryMapper;
import com.jd.dao.MovieMapper;
import com.jd.dao.MoviecategoryMapper;
import com.jd.pojo.*;
import com.jd.service.MovieService;
import com.jd.vo.NewMovie;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MovieServiceImpl implements MovieService{

    @Autowired
    private MovieMapper movieMapper;

    @Autowired
    private MoviecategoryMapper moviecategoryMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public Page selectMovieList(Query query) {

        Page<NewMovie> page = new Page<>();
        page.setSize(10);
        query.setSize(10);
        if (null != query) {
            //判断当前页
            if (null != query.getPage()) {
                page.setPage(query.getPage());
                query.setStartRow((query.getPage() - 1) * query.getSize());
            }
            if (null != query.getMovieName() && !"".equals(query.getMovieName().trim())) {
                query.setMovieName(query.getMovieName().trim());
            }
            if (0 != (query.getCategoryId())) {
                query.setCategoryId(query.getCategoryId());
            }
            System.out.println("query: " + JSON.toJSONString(query));

            page.setTotal(movieMapper.movieCount(query) );
            List<NewMovie> newMovieList = new ArrayList<>();
            List<Movie> movieList =  movieMapper.selectMovieListByQuery(query);
            System.out.println("movieList: " + JSON.toJSONString(movieList));
            for (Movie movie: movieList) {
                NewMovie newMovie = getMovieById(movie.getMovieid());
                newMovieList.add(newMovie);
            }
            page.setRows(newMovieList);
            System.out.println("page: " + JSON.toJSONString(page));
        }
        return page;
    }

    @Override
    public List<Category> selectCategory() {

        CategoryExample example = new CategoryExample();
        List<Category> categoryList = categoryMapper.selectByExample(example);
        return categoryList;
    }

    @Override
    public void deleteMovie(Integer id) {

        MoviecategoryExample example = new MoviecategoryExample();
        MoviecategoryExample.Criteria criteria = example.createCriteria();
        criteria.andMovieidEqualTo(id);
        moviecategoryMapper.deleteByExample(example);
        movieMapper.deleteByPrimaryKey(id);
    }

    @Override
    public NewMovie selectMovieById(Integer id) {

        NewMovie newMovie = new NewMovie();
        //根据电影id获取该条电影记录并给newMovie
        Movie movie = movieMapper.selectByPrimaryKey(id);
        newMovie.setMovieid(movie.getMovieid());
        newMovie.setMoviename(movie.getMoviename());
        newMovie.setShowyear(movie.getShowyear());
        newMovie.setDirector(movie.getDirector());
        newMovie.setLeadactors(movie.getLeadactors());
        newMovie.setPicture(movie.getPicture());
        newMovie.setAverating(movie.getAverating());
        newMovie.setNumrating(movie.getNumrating());
        newMovie.setDescription(movie.getDescription());
        newMovie.setBackpost(movie.getBackpost());

        System.out.println("movie:： " + JSON.toJSONString(movie));
        System.out.println("newmOvie: " + JSON.toJSONString(newMovie));

        //根据电影id查询电影对应的类别
        MoviecategoryExample example = new MoviecategoryExample();
        MoviecategoryExample.Criteria criteria = example.createCriteria();
        criteria.andMovieidEqualTo(id);
        List<Moviecategory> list = moviecategoryMapper.selectByExample(example);

        List temps = new ArrayList();
        //
        String categoryname = " ";
        for(Moviecategory mc:list){
            int temId = mc.getCategoryid();
            temps.add(temId);
            Category category = categoryMapper.selectByPrimaryKey(temId);
            categoryname = categoryname + category.getCategory() + "/" ;
        }

        //list转为数组并放到newMovie对象中
        Integer[] arrs =  (Integer[]) temps.toArray(new Integer[temps.size()]);
        newMovie.setCategoryid(arrs);
        newMovie.setCategoryname(categoryname);
        return newMovie;
    }


    @Override
    public void updateMovie(Movie movie, String[] categoryIds) {

        movieMapper.updateByPrimaryKey(movie);
        Integer movieid = movie.getMovieid();

        // 删除原来的电影分类,再加入
        MoviecategoryExample example = new MoviecategoryExample();
        MoviecategoryExample.Criteria criteria = example.createCriteria();
        criteria.andMovieidEqualTo(movieid);
        moviecategoryMapper.deleteByExample(example);

        for(String categoryId: categoryIds) {
           Moviecategory moviecategory = new Moviecategory();
           int tempcategoryId = Integer.parseInt(categoryId);
           moviecategory.setMovieid(movieid);
           moviecategory.setCategoryid(tempcategoryId);
           moviecategoryMapper.insert(moviecategory);

        }
    }


    @Override
    public void addMovie(Movie movie, String[] categoryIds) {

        //将电影插入到电影表中
        movieMapper.insert(movie);
        //将电影所属分类插到电影类目表中
        System.out.println("movies? " + JSON.toJSONString(movie));
        int movieid = movie.getMovieid();
        for (String categoryId: categoryIds) {
            Moviecategory moviecategory = new Moviecategory();
            int tempcategoryId = Integer.parseInt(categoryId);
            moviecategory.setMovieid(movieid);
            moviecategory.setCategoryid(tempcategoryId);
            moviecategoryMapper.insert(moviecategory);
        }

    }


    //根据电影id获取该条电影记录(包括类别）
    private NewMovie getMovieById(Integer movieid) {

        NewMovie newMovie = new NewMovie();
        //根据电影id获取该条电影记录并给newMovie
        Movie movie = movieMapper.selectByPrimaryKey(movieid);
        newMovie.setMovieid(movie.getMovieid());
        newMovie.setMoviename(movie.getMoviename());
        newMovie.setShowyear(movie.getShowyear());
        newMovie.setDirector(movie.getDirector());
        newMovie.setLeadactors(movie.getLeadactors());
        newMovie.setPicture(movie.getPicture());
        newMovie.setAverating(movie.getAverating());
        newMovie.setNumrating(movie.getNumrating());
        newMovie.setDescription(movie.getDescription());

        //根据电影id查询电影对应的类别
        MoviecategoryExample example = new MoviecategoryExample();
        MoviecategoryExample.Criteria criteria = example.createCriteria();
        criteria.andMovieidEqualTo(movieid);
        List<Moviecategory> list = moviecategoryMapper.selectByExample(example);
        List temps = new ArrayList();

        String categoryname = " ";
        //将符合条件的id放到list里
        for(Moviecategory mc:list){
            int temId = mc.getCategoryid();
            temps.add(temId);
            Category category = categoryMapper.selectByPrimaryKey(temId);
            categoryname = categoryname + category.getCategory() + "/" ;
        }

        //list转为数组并放到newMovie对象中
        Integer[] arrs =  (Integer[]) temps.toArray(new Integer[temps.size()]);
        newMovie.setCategoryid(arrs);

        // 设置newMovie的category名称
        newMovie.setCategoryname(categoryname);
        return newMovie;

    }


}
